<template>
  <div class="stationCard" ref="stationCardRef">
    <div
      :class="['card', `carde-${item.statusClassName}`]"
      v-for="(item, index) in props.stationInfList"
      :key="index"
      @click="goToMonitorStaion(item.stationId, MONITOR_TAB_ENUM.Running)"
    >
      <!-- <div :class="['card-stationStatus' ]"></div> -->
      <div class="card-header">
        <div class="card-title flex-start a-center">
          <div class="heading-title">{{ item.stationName }}</div>
          <div
            :class="['card-stationStatus', 'ml5', item.statusClassName]"
            v-if="item.status !== 3"
          ></div>
        </div>
        <div class="flex-center a-center">
          <div class="subtitle-title mr15">
            <span class="timeTitle">投运时间：</span
            >{{ item.companyCreateTime }}
          </div>
          <div
            class="icon-button nom first"
            @click.stop="toTop(item.stationId)"
          ></div>
        </div>
      </div>
      <!-- <div class="card-header">
        <div
          class="card-shorter"
          :style="{ 'font-size': `${item.companyShortNmaeStyle}px` }"
        >
          {{ item.stationShortName }}
        </div>
        <div class="card-title">
          <div class="heading-title">{{ item.stationName }}</div>
          <div class="subtitle-title">
            投运时间：{{ item.companyCreateTime }}
          </div>
        </div>
        <div
          class="icon-button nom first"
          @click.stop="toTop(item.stationId)"
        ></div>
      </div> -->
      <div class="card-bottom">
        <div class="card-content">
          <div class="data-row">
            <div>
              项目容量: <span class="blue">{{ item.capacity }}</span
              >MWh
            </div>
            <div>
              已安全运行:
              <span class="blue">{{
                item.status === 3 ? item.runnDays : "--"
              }}</span
              >天
            </div>
          </div>
          <div class="data-row">
            <div>
              累计放电量:
              <span class="blue"
                >{{ (item.stationTotalDischarge / 10).toFixed(1) }}万kwh</span
              >
            </div>
            <div>
              累计节费: <span class="blue">{{ item.totalSaveCost }}</span
              >万元
            </div>
          </div>
        </div>
        <div class="card-status">
          <div class="status-item">
            <div class="status-icon">
              <i class="fas fa-wifi"></i>
              <span class="status-num" v-if="item.offlineNumber > 0">{{
                item.offlineNumber
              }}</span>
            </div>
            <span>离线状态</span>
          </div>
          <div class="status-item">
            <div class="status-icon charging">
              <i class="fas fa-bolt"></i>
              <span
                class="status-font"
                v-if="item.CDStatus && item.CDStatus !== null"
                >{{ item.CDStatus }}</span
              >
            </div>
            <span>充放情况</span>
          </div>
          <div
            class="status-item"
            @click.stop="
              goToMonitorStaion(item.stationId, MONITOR_TAB_ENUM.Error)
            "
          >
            <div class="status-icon">
              <i class="fas fa-lightbulb"></i>
              <span class="status-num" v-if="item.alarmNumber > 0">{{
                item.alarmNumber
              }}</span>
            </div>
            <span>设备告警</span>
          </div>
          <div class="status-item" @click.stop="goToWorkOrder(item.stationId)">
            <div class="status-icon">
              <i class="fas fa-file-alt"></i>
              <span class="status-num" v-if="item.workOrderNumber > 0">{{
                item.workOrderNumber
              }}</span>
            </div>
            <span>项目工单</span>
          </div>
        </div>
        <div class="add-up flex-center a-center">
          <div style="width: 20px">soc：</div>
          <div class="flex1" style="padding-left: 14px">
            <div :class="['card-progress']">
              <div
                :class="[item.soc > 0 ? 'progress-bar' : '']"
                :style="{ width: `${item.soc}%` }"
              >
                <span style="position: relative; z-index: 2"
                  >{{ item.soc }}%</span
                >
              </div>
            </div>

            <div class="card-footer">
              <!--  <span
              >SOC: <span style="color: #efa16b">{{ item.soc }}</span
              >%</span
            > -->
            </div>
          </div>
        </div>
        <!-- <div class="add-up">
          <div
            :class="[
              'card-progress',
              !item.totalChargeProgress || item.totalChargeProgress === 0
                ? 'gray'
                : '',
            ]"
          >
            <div
              :class="[item.totalChargeProgress > 0 ? 'progress-bar' : '']"
              :style="{ width: `${item.totalChargeProgress}%` }"
            ></div>
          </div>

          <div class="card-footer">
            <span
              >累计充电量:
              <span style="color: #efa16b">{{ item.stationTotalCharge }}</span
              >MWh</span
            >
            <span
              >累计放电量:
              <span style="color: #efa16b">{{
                item.stationTotalDischarge
              }}</span
              >MWh</span
            >
          </div>
        </div>
        <div class="month-up">
          <div
            :class="[
              'card-progress',
              !item.monthChargeProgress || item.monthChargeProgress === 0
                ? 'gray'
                : '',
            ]"
          >
            <div
              :class="[item.monthChargeProgress > 0 ? 'progress-bar' : '']"
              :style="{ width: `${item.monthChargeProgress}%` }"
            ></div>
          </div>

          <div class="card-footer">
            <span
              >本月充电量:<span style="color: #338be4">{{
                item.monthCharge
              }}</span
              >MWh</span
            >
            <span
              >本月放电量:
              <span style="color: #338be4">{{ item.monthDischarge }}</span
              >MWh</span
            >
          </div>
        </div> -->
      </div>
    </div>
    <!-- <div v-if="props.loading" class="loading-more">
      <a-spin />
      <span>加载中...</span>
    </div>
    <div
      v-if="!props.hasMore && props.stationInfList.length > 0"
      class="no-more-data"
    >
      没有更多数据了
    </div> -->
  </div>
</template>
<script setup>
import { MONITOR_TAB_ENUM, ERROR_TAB_ENUM } from "@/constants/stationConfig"
const router = useRouter()
const $api = inject("$api")
const props = defineProps({
  stationInfList: {
    type: Array,
  },
  hasMore: {
    type: Boolean,
    default: true,
  },
  loading: {
    type: Boolean,
    default: false,
  },
})
const emits = defineEmits(["getStationOverview", "loadMore"])
const toTop = async (id) => {
  const res = await $api.stationToTop({ stationId: id })
  if (res.code === 0) {
    emits("getStationOverview")
  }
}
const goToMonitorStaion = async (id, type) => {
  router.push({
    path: "/MonitoringCenter/MonitorStaion",
    query: {
      stationId: id,
      pageType: type,
    },
  })
}
const goToWorkOrder = async (id) => {
  // 跳到单站管理-故障监控-未完成工单
  router.push({
    path: "/MonitoringCenter/MonitorStaion",
    query: {
      stationId: id,
      pageType: MONITOR_TAB_ENUM.Error,
      errorType: ERROR_TAB_ENUM.WorkOrder,
    },
  })
}
onMounted(() => {})

// 滚动加载相关
const stationCardRef = ref(null)
const scrollHandler = () => {
  if (!props.hasMore || props.loading) return

  const scrollContainer = document.documentElement || document.body
  const scrollTop = scrollContainer.scrollTop
  const scrollHeight = scrollContainer.scrollHeight
  const clientHeight = scrollContainer.clientHeight
  console.log(scrollHeight, scrollTop, clientHeight)
  // 当滚动到距离底部100px时触发加载更多
  if (scrollHeight - scrollTop - clientHeight < 100) {
    emits("loadMore")
  }
}

onMounted(() => {
  // 添加滚动事件监听
  window.addEventListener("scroll", scrollHandler)
})

onUnmounted(() => {
  // 移除滚动事件监听
  window.removeEventListener("scroll", scrollHandler)
})
</script>
<style lang="less" scoped>
@import "@/assets/index.css";
.stationCard {
  display: flex;
  flex-wrap: wrap;
  justify-content: start;
  gap: 30px 20px;

  .card {
    //border: 1px solid #eee;
    color: #fff;
    border-radius: 8px;
    //padding: 15px;
    background: #08111b;
    box-shadow: 0 2px 10px rgba(5, 9, 13, 0.8);
    display: flex;
    flex-direction: column;
    position: relative;
    // overflow: hidden;
    cursor: pointer;
    margin: 0;
    width: calc(25% - 15px);
    min-width: 300px;
    max-width: calc(25% - 15px);
    @media (min-width: 1740px) and (max-width: 1905px) {
      .timeTitle {
        display: none;
      }
    }
    @media (min-width: 1350px) and (max-width: 1486px) {
      .timeTitle {
        display: none;
      }
    }
    @media screen and (max-width: 1740px) {
      width: calc(33.33% - 14px);
      max-width: calc(33.33% - 14px);
    }

    @media screen and (max-width: 1350px) {
      width: calc(50% - 10px);
      max-width: calc(50% - 10px);
    }

    @media screen and (max-width: 768px) {
      width: 100%;
      max-width: 100%;
    }

    &.carde-stop {
      filter: grayscale(100%);
    }
    &:first-child {
      margin-left: 0;
      .icon-button {
        &.nom {
          background: url("/src/assets/overview11.png") no-repeat center center !important;
        }
      }
    }
    &:nth-child(4n + 1) {
      margin-left: 0 !important;
    }
    .card-stationStatus {
      width: 45px;
      height: 19px;

      /*  position: absolute;
      top: 0;
      left: 0;
      z-index: 2; */
      &.building {
        background: url("/src/assets/overview14.png") no-repeat center / 100%
          100%;
      }
      &.stop {
        background: url("/src/assets/overview15.png") no-repeat center / 100%
          100%;
      }
      &.toBuilding {
        background: url("/src/assets/overview16.png") no-repeat center / 100%
          100%;
      }
    }
    .card-header {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      margin-bottom: 10px;
      position: relative;
      top: -4px;
      left: -2px;
      width: 101%;
      background: url("/src/assets/topBg.png") no-repeat center center/100% auto;
      height: 49px;

      /* .card-shorter {
        margin: 0;
        font-weight: 500;
        width: 50px;
        height: 50px;
        background: #eee;
        color: #1c28b8;
        display: flex;
        justify-content: center;
        align-items: center;
        text-align: center;
      } */
      .card-title {
        color: #eee;
        padding-left: 40px;
        flex: 1;
        line-height: 43px;
        .heading-title {
          font-size: 18px;
          //margin: 0 0 10px 0;
          font-weight: 700;
        }
      }
      .subtitle-title {
        font-size: 12px;
        color: #babfc4;
      }
      .status-tag {
        font-size: 12px;
        padding: 2px 5px;
        border-radius: 4px;
        margin-left: 5px;
        &.high-voltage {
          background-color: #ff9800;
          color: #fff;
        }
        &.self-owned {
          background-color: #03a9f4;
          color: #fff;
        }
        &.storage {
          background-color: #4caf50;
          color: #fff;
        }
        &.under-construction {
          background-color: #9c27b0;
          color: #fff;
        }
        &.stopped {
          background-color: #f44336;
          color: #fff;
        }
      }
      .icon-button {
        border: none;
        color: #fff;
        font-size: 18px;
        cursor: pointer;
        padding: 0;
        width: 17px;
        height: 17px;
        border-radius: 50%;
        margin-right: 10px;
        /*  position: absolute;
        right: 0;
        top: 0; */
        &.nom {
          background: url("/src/assets/overview10.png") no-repeat center center;
        }
        &.first {
          //background: url("/src/assets/overview11.png") no-repeat center center;
        }
      }
    }
    .card-bottom {
      padding: 0 15px 15px 15px;
    }
    .card-content {
      font-size: 12px;
      margin-bottom: 10px;
      p {
        margin: 3px 0;
      }
      .data-row {
        display: flex;
        justify-content: space-between;
        div {
          flex: 1;
          height: 39px;
          background: url("/src/assets/conBg.png") no-repeat center center /
            100% 100%;
          color: #fff;
          display: flex;
          align-items: center;
          justify-content: start;
          padding-left: 28px;
        }
        &:first-child {
          margin-bottom: 10px;
        }
        .blue {
          font-weight: bolder;
          color: #2993ff;
          &::before {
            content: "";
            display: inline-block;
            width: 9px;
            height: 11px;
            background: url("/src/assets/fontBf.png") no-repeat center center /
              100% 100%;
            margin-right: 3px;
            margin-left: 5px;
          }
        }
      }
    }
    .card-status {
      display: flex;
      justify-content: space-around;
      align-items: center;
      margin: 10px 0 15px 0;
      .status-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        i {
          font-size: 24px;
          margin-bottom: 5px;
        }
        span {
          font-size: 12px;
        }
        .status-icon {
          display: flex;
          align-items: center;
          justify-content: center;
          margin-bottom: 5px;
          position: relative;
          .status-num {
            position: absolute;
            top: -5px;
            right: -8px;
            display: block;
            width: 20px;
            height: 20px;
            background-color: #ff6711;
            border-radius: 20px;
            text-align: center;
            line-height: 20px;
            font-size: 12px;
          }
          .status-font {
            position: absolute;
            top: -10px;
            right: 50%;
            margin-right: -25px;
            display: block;
            background-color: #fff;
            font-size: 12px;
            width: 50px;
            height: 20px;
            color: #349be0;
            font-weight: 700;
            text-align: center;
            line-height: 20px;
            border-radius: 10px;
            border: 1px solid #349be0;
          }
          i {
            display: block;
            width: 40px;
            height: 40px;
            background-color: #eee !important;
            border-radius: 50%;
          }
          .fa-wifi {
            background: url("/src/assets/overview8.png") no-repeat center
              center/100% 100%;
          }
          .fa-bolt {
            background: url("/src/assets/overview6.png") no-repeat center
              center/100% 100%;
          }
          .fa-lightbulb {
            background: url("/src/assets/overview7.png") no-repeat center
              center/100% 100%;
          }
          .fa-file-alt {
            background: url("/src/assets/overview2.png") no-repeat center
              center/100% 100%;
          }
        }
      }
    }

    .card-progress {
      background-color: #19314a;
      height: 14px;
      border-radius: 14px;
      margin-bottom: 10px;
      position: relative;
      margin-top: 12px;

      //overflow: hidden;
      .progress-bar {
        background-color: #9ee6fe;
        height: 100%;
        border-radius: 14px;
        position: absolute;
        left: 0;
        top: 0;
        text-align: right;
        padding-right: 12px;
        font-size: 12px;
        color: #423016;

        &::before {
          content: "";
          position: absolute;
          top: 50%;
          right: -24px;
          transform: translateY(-58%);
          width: 48px;
          height: 50px;
          background: url("/src/assets/bar2.png") no-repeat center center/100%
            100%;
        }
      }
      &.gray {
        background-color: #dedede !important;
      }
    }
    .add-up {
      margin-top: 10px;
    }
    .month-up {
      margin-top: 20px;
      .card-progress {
        background-color: #204977;
        &.gray {
          background-color: #dedede !important;
        }
        .progress-bar {
          background-color: #338be4;
          &::before {
            background: url("/src/assets/bar2.png") no-repeat center center/100%
              100%;
          }
        }
      }
    }
    .card-footer {
      display: flex;
      justify-content: space-between;
      font-size: 12px;
      width: 100%;
      span {
        color: #ccc;
      }
      .footer-data {
        display: flex;
        flex-direction: column;
        span:first-child {
          color: #fff;
        }
        :last-child {
          color: #ccc;
        }
      }
    }
    .tag-container {
      position: absolute;
      top: 10px;
      right: 10px;
      .tag {
        background-color: #9c27b0;
        color: #fff;
        padding: 2px 5px;
        border-radius: 4px;
        font-size: 10px;
        &.fault-tag {
          background-color: #f44336;
        }
      }
    }
  }

  .loading-more {
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px 0;
    color: #94a3b8;
    gap: 10px;
  }

  .no-more-data {
    width: 100%;
    text-align: center;
    padding: 20px 0;
    color: #94a3b8;
    font-size: 14px;
  }
}
</style>
